$(function() {
    //首页进去时
$('#libtn').click(function(){
    $('#waterfull ul').empty();
    var e = 2;
    $.ajax({
        url:'/client/user/seeCollection?page=1&e='+e,
        data:{},
        type:'GET',
        dateType:'json',
        success:function(res){
            //解析res
            var obj = eval("("+res+")");
            console.log(obj);
            if(obj.error == 0){
                //判断是否加载完毕
                if (obj.num == 0 || last) {
                    last = true;
                    layer.msg('没有更多作品！');
                    return;
                }
                //加载 成功函数
                toSuccess(obj,page);

                page++;
            }else{
                layer.msg(obj.msg);
            }
        },
        error:function(){
            layer.msg('error');
        }
    });

    /*****************************************************/
    var page = 1,
        last = false;
    $(window).scroll(function () {//alert(21);
        if(($(window).scrollTop() + $(window).height()) == $(document).height()){
            if (page > 1) {
                $.ajax({
                    type: "GET",
                    url: '/client/user/seeCollection?page='+page+'&e='+e,
                    data: {},
                    async:false,
                    success: function(res) {
                        //解析res
                        var obj = eval("("+res+")");
                        console.log(obj);
                        if(obj.error == 0){
                            //判断是否加载完毕
                            if (obj.num == 0 || Math.ceil(obj.num/e) < page || last == true) {
                                layer.msg('没有更多作品！');
                                return;
                            }
                            //加载 成功函数
                            toSuccess(obj,page);
                            //页数超过总页  停止加载
                            page++;
                        }else{
                            layer.msg(obj.msg);
                        }
                    },
                    error: function() {
                        alert("network error");
                    }
                });
            }
        }
    });
});
});
function toSuccess(obj,page){

    var boxes = new Array;
    var sqlJson = obj.list;
    var html="";
    for(var i=0;i<sqlJson.length;i++){
        html+="<li class='item' id='item"+page+i+"'><a onclick='opena("+sqlJson[i].id+")'  class='a-img'><img src='/productImg/"+sqlJson[i].picture+"'></a>";
        html+="<div class='choose'><div class='collect'><a onclick='cancelCollection("+sqlJson[i].id+","+sqlJson[i].pid+","+page+","+i+")'><span onclick='toCancel()'><img src='/client/image/delete.png''></span></a></div></div>";
        html+="<h2 class='li-title'>"+sqlJson[i].name+"</h2>";
        html+="<p class='description'>"+sqlJson[i].brief+"</p><div class='qianm clearfloat'>";
        html+="<div class='user-block'><div class='user' ><img style='border-radius:50%' src='/portraitImg/"+sqlJson[i].artist_portrait+"'></div></div><div class='user-introduction'>";
        html+="<div class='user-name'>"+sqlJson[i].artist_name+"</div><div class='user-skill'>"+sqlJson[i].artist_content+"</div> </div></div></li>";
    }
    var div = $('#waterfull ul');
    div.append(html);
    boxes.push(div);
    $("#demo").gridalicious('append', boxes);
}
//打开详情页
function opena(_id){
    layer.open({type: 2,area: ['800px', '90%'], skin: 'layui-layer-rim', content: ['/client/productDetails/'+_id]});

}
function cancelCollection(_id,_pid,page,i){
    if(confirm("确定要取消收藏吗？")) {

    } else {
        return false;
    }
    $.ajax({
        url:'/client/user/cancelCollection',
        data:{id:_id,pid:_pid},
        type:'GET',
        dateType:'json',
        async:false,
        success:function(res){
            var res = eval("("+res+")");
            console.log(res);
            if(res.error == 0){
                $('#item'+page+i).hide();
            }else{
                layer.msg(res.msg);
            }

        },
        error:function(){
            layer.msg('error');
        }
    });
}